﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;
using Telerik.Web.UI;
using System.Collections;



public static class ItemsDAL
{
    private static ItemsDataContext context;

    static ItemsDAL()
    {
        context = new ItemsDataContext();
    }

    // The id of the last entered record in table legts
    public static int GetLastLegId()
    {
        int lastRecordId = (from i in context.Legs
                            select i.LegID).Max();
        return lastRecordId;
    }
    
    // Delete leg by id
    public static void DeleteLegById(int legID)
    {
        Leg leg = (from l in context.Legs
                   where l.LegID == legID
                   select l).FirstOrDefault<Leg>();

        context.Legs.DeleteOnSubmit(leg);
        try
        {
            context.SubmitChanges();
        }
        catch (ChangeConflictException)
        {
            context.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges);
        }
    }

    // Delete cover by id
    public static void DeleteCoverById(int coverID)
    {
        Cover cover = (from c in context.Covers
                       where c.CoverID == coverID
                       select c).FirstOrDefault<Cover>();

        context.Covers.DeleteOnSubmit(cover);
        try
        {
            context.SubmitChanges();
        }
        catch (ChangeConflictException)
        {
            context.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges);
        }
    }


    // This is the sum of the prices of all bufabs applied to a given leg
    public static decimal? GetBufabSum(int legID)
    {
        decimal? sum = 0;

        List<FinalProduct> materials = (from p in context.FinalProducts
                                        where p.LegID == legID
                                        select p).ToList<FinalProduct>();

        for (int i = 0; i != materials.Count; i++)
        {
            sum += materials[i].Material.Price;
        }

        return sum;
    }

    // Get color by id
    public static Color GetColorById(int id)
    {
        Color color = (from c in context.Colors
                       where c.ColorID == id
                       select c).FirstOrDefault<Color>();
        return color;
    }

    // Get type by id
    public static Type GetTypeById(int id)
    {
        Type type = (from t in context.Types
                     where t.TypeID == id
                     select t).FirstOrDefault<Type>();
        return type;
    }

    // Get all the bufabs applid to a given leg
    public static IQueryable<Material> GetBufabs(int legID)
    {
        IQueryable<Material> materials = from m in context.FinalProducts
                                         where m.LegID == legID
                                         select m.Material;

        return materials;
    }

    // Get the id of the last entered cover
    public static int GetLastCoverId()
    {
        int id = (from c in context.Covers
                  select c.CoverID).Max();

        return id;
    }

    // Insert item in table Final Cover
    public static void InsertFinalCover(int coverId, int materialId)
    {
        FinalCover cover = new FinalCover()
        {
            CoverID = coverId,
            MaterialID = materialId
        };

        context.FinalCovers.InsertOnSubmit(cover);

        try
        {
            context.SubmitChanges();
        }

        catch (ChangeConflictException)
        {
            context.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges);
        }
    }

    // Get material by id 
    public static Material GetMaterialById(int id)
    {
        Material material = (from m in context.Materials
                             where m.MaterialID == id
                             select m).Single<Material>();

        return material;
    }

    // Get list of all materials that are applied to a given cover  
    public static IQueryable<Material> GetMaterialsForFinalCovers(int coverId)
    {
        IQueryable<Material> materials = from m in context.FinalCovers
                                         where m.CoverID == coverId
                                         select m.Material;

        return materials;
    }

    // Delete final cover by id
    public static void DeleteFinalCover(int materialId, int coverId)
    {
        FinalCover finalCover = (from m in context.FinalCovers
                                 where m.MaterialID == materialId && m.CoverID == coverId
                                 select m).First<FinalCover>();

        context.FinalCovers.DeleteOnSubmit(finalCover);

        try
        {
            context.SubmitChanges();
        }

        catch (ChangeConflictException)
        {
            context.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges);
        }
    }

    // Get the last entered claim
    public static IQueryable<Claim> GetLastEnteredClaim()
    {
        int id = (from c in context.Claims
                  select c.ClaimId).Max<int>();

        IQueryable<Claim> claim = from c in context.Claims
                                  where c.ClaimId == id
                                  select c;

        return claim;
    }

    // Check whether an element with a given code exists
    public static bool ElementExists(string code)
    {
        foreach (var leg in context.Legs)
        {
            if (leg.Code == code)
            {
                return true;
            }
        }

        foreach (var cover in context.Covers)
        {
            if (cover.Code == code)
            {
                return true;
            }
        }

        return false;


    }
    // Get the name of an item by given code
    public static string GetNameByCode(string code)
    {
        foreach (var leg in context.Legs)
        {
            if (leg.Code == code)
            {
                return leg.NameBG;
            }
        }

        foreach (var cover in context.Covers)
        {
            if (cover.Code == code)
            {
                return cover.NameBG;
            }
        }

        return null;
    }

    // Get the price of an item by given code
    public static decimal? GetPriceByCode(string code)
    {
        foreach (var leg in context.Legs)
        {
            if (leg.Code == code)
            {
                return leg.EXW3;
            }
        }

        foreach (var cover in context.Covers)
        {
            if (cover.Code == code)
            {
                return cover.DDU;
            }
        }

        return null;
    }

    // Insert rejected item in the DB
    public static void InsertRejectedItem(RejectedItem item)
    {
        context.RejectedItems.InsertOnSubmit(item);
        try
        {
            context.SubmitChanges();
        }
        catch (ChangeConflictException)
        {
            context.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges);
        }
    }

    public static void InsertClaim(Claim claim)
    {
        context.Claims.InsertOnSubmit(claim);
        try
        {
            context.SubmitChanges();
        }
        catch (ChangeConflictException)
        {
            context.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges);
        }
    }

    // Get the id of the last entered claim
    public static int GetLastClaimId()
    {
        int id = (from c in context.Claims
                  select c.ClaimId).Max<int>();

        return id;
    }

    // Get claim by id
    public static Claim GetClaimById(int id)
    {
        Claim claim = (from c in context.Claims
                       where c.ClaimId == id
                       select c).FirstOrDefault<Claim>();

        return claim;
    }

    // Get supplier name by id
    public static string GetSuppNameById(int? id)
    {
        string name = (from s in context.Suppliers
                       where s.SupplierID == id
                       select s.Name).FirstOrDefault<string>();

        return name;
    }

    // Get client name by id
    public static string GetClientNameById(int? id)
    {
        string name = (from c in context.Customers
                       where c.CustomerID == id
                       select c.Name).FirstOrDefault<string>();

        return name;
    }

    // Get supplied address by id
    public static string GetSuppAddressById(int? id)
    {
        string address = (from c in context.Customers
                       where c.CustomerID == id
                       select c.AddressCorrespondence).FirstOrDefault<string>();

        return address;
    }

    // return status name by status id

    public static string GetStatusName(int? id)
    {
        string name = (from c in context.Status
                       where c.id == id
                       select c.status1).FirstOrDefault<string>();

        return name;
    }

    #region GetTelerikGridSelections
    //Get Selected Values From RadGrid and return ArrayList
    public static ArrayList GetTelerikGridSelections(Telerik.Web.UI.RadGrid grid)
    {
        ArrayList selectedItems = new ArrayList();
        if (grid.MasterTableView.DataKeyNames.Length > 0)
        {
            string key = grid.MasterTableView.DataKeyNames[0];
            for (int i = 0; i < grid.SelectedItems.Count; i++)
            {
                selectedItems.Add(grid.MasterTableView.DataKeyValues[grid.SelectedItems[i].ItemIndex][key]);
            }
        }
        return selectedItems;
    }
    #endregion 
}
